package cn.com.inossem.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.RetrieveInfoBean;
import cn.com.inossem.model.RetrieveInfoModel;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.Utils;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 检索向导
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class RetrieveActivity extends RequestActivity {
	protected Context context = RetrieveActivity.this;
	RetrieveInfoBean retrieveinfo;
	public List<RetrieveInfoModel> listproject;
	public List<RetrieveInfoModel> listindustry;
	public List<RetrieveInfoModel> listmodel;
	public List<RetrieveInfoModel> listjobs;

	public List<List<TextView>> listview;
	// List<View> listtextview;
	LinearLayout linlayer, linlayer1, linlayer2, linlayer3;
	LayoutInflater mInflater;
	TextView next;
	ImageView back;

	List<RetrieveInfoModel> xuanzhongProject;// 选中数据
	List<RetrieveInfoModel> xuanzhongIndustry;// 选中数据
	List<RetrieveInfoModel> xuanzhongModel;// 选中数据
	List<RetrieveInfoModel> xuanzhongJobs;// 选中数据

	// private StatusEnum statusEnum;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);
		initView();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {

		listview = new ArrayList<List<TextView>>();
		xuanzhongProject = new ArrayList<RetrieveInfoModel>();
		xuanzhongIndustry = new ArrayList<RetrieveInfoModel>();
		xuanzhongModel = new ArrayList<RetrieveInfoModel>();
		xuanzhongJobs = new ArrayList<RetrieveInfoModel>();

		next = (TextView) this.findViewById(R.id.next);
		next.setOnClickListener(this);
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		listproject = new ArrayList<RetrieveInfoModel>();
		listindustry = new ArrayList<RetrieveInfoModel>();
		listmodel = new ArrayList<RetrieveInfoModel>();
		listjobs = new ArrayList<RetrieveInfoModel>();

		selectData_info(listproject, "t_dictionary", "pid=109", 1);
		selectData_info(listindustry, "t_dictionary", "pid=108", 2);
		selectData_info(listmodel, "t_dictionary", "pid=126", 3);
		selectData_info(listjobs, "t_dictionary", "pid=127", 4);

		linlayer = (LinearLayout) this.findViewById(R.id.linlayer);
		addView(linlayer, listproject, 1);
		linlayer1 = (LinearLayout) this.findViewById(R.id.linlayer1);
		addView(linlayer1, listindustry, 2);
		linlayer2 = (LinearLayout) this.findViewById(R.id.linlayer2);
		addView(linlayer2, listmodel, 3);
		linlayer3 = (LinearLayout) this.findViewById(R.id.linlayer3);
		addView(linlayer3, listjobs, 4);
	}

	private void addView(LinearLayout layer, List<RetrieveInfoModel> listrim,
			final int type) {
		List<TextView> listText = new ArrayList<TextView>();

		List<View> listtextview = new ArrayList<View>();
		for (int i = 0; i < listrim.size(); i++) {
			mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.textviewitem, null);
			String text = listrim.get(i).getName();
			TextView textview = (TextView) view.findViewById(R.id.textview);
			if (listrim.get(i).getIsselected() == 0) {
				textview.setTextColor(Color.rgb(66, 66, 66));
				if (i == listrim.size() - 1)
					textview.setBackgroundResource(R.drawable.selectbg);
				else
					textview.setBackgroundResource(R.drawable.weixuanzhong);
			} else {
				textview.setTextColor(Color.rgb(255, 255, 255));
				textview.setBackgroundResource(R.drawable.xuanzhong);
				// 初始化选中数据
				switch (type) {
				case 1:
					xuanzhongProject.add(listrim.get(i));
					break;
				case 2:
					xuanzhongIndustry.add(listrim.get(i));
					break;
				case 3:
					xuanzhongModel.add(listrim.get(i));
					break;
				case 4:
					xuanzhongJobs.add(listrim.get(i));
					break;
				default:
					break;
				}
			}

			textview.setText(text);
			textview.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					TextView tv = (TextView) v;
					int posion = (Integer) tv.getTag();
					switch (type) {
					case 1:
						if (listproject.get(posion).getIsselected() == 0) {
							if (posion == listproject.size() - 1) {
								for (int k = 0; k < listproject.size(); k++) {
									listproject.get(k).setIsselected(1);
									TextView textView = listview.get(0).get(k);
									textView.setBackgroundResource(R.drawable.xuanzhong);
									textView.setTextColor(Color.rgb(255, 255,
											255));
									if (!xuanzhongProject.contains(listproject
											.get(k))) {
										xuanzhongProject.add(listproject.get(k));
									}
								}
							} else {
								listproject.get(posion).setIsselected(1);
								tv.setTextColor(Color.rgb(255, 255, 255));
								tv.setBackgroundResource(R.drawable.xuanzhong);
								if (!xuanzhongProject.contains(listproject
										.get(posion))) {
									xuanzhongProject.add(listproject
											.get(posion));
								}
							}
						} else {
							if (posion == listproject.size() - 1) {
								for (int k = 0; k < listproject.size(); k++) {
									listproject.get(k).setIsselected(0);
									TextView textView = listview.get(0).get(k);
									textView.setTextColor(Color.rgb(66, 66, 66));
									if (k == listproject.size() - 1)
										textView.setBackgroundResource(R.drawable.selectbg);
									else
										textView.setBackgroundResource(R.drawable.weixuanzhong);
									if (xuanzhongProject.contains(listproject
											.get(k))) {
										xuanzhongProject.remove(listproject
												.get(k));
									}
								}
							} else {
								listproject.get(posion).setIsselected(0);
								tv.setTextColor(Color.rgb(66, 66, 66));
								if (posion == listproject.size() - 1) {
									tv.setBackgroundResource(R.drawable.selectbg);
								} else
									tv.setBackgroundResource(R.drawable.weixuanzhong);
								if (xuanzhongProject.contains(listproject
										.get(posion))) {
									xuanzhongProject.remove(listproject
											.get(posion));
								}
								if (xuanzhongProject.contains(listproject
										.get(listproject.size() - 1))) {
									TextView textView = listview.get(0).get(
											listview.get(0).size() - 1);
									listproject.get(listproject.size() - 1)
											.setIsselected(0);
									textView.setTextColor(Color.rgb(66, 66, 66));
									textView.setBackgroundResource(R.drawable.weixuanzhong);
									xuanzhongProject.remove(listproject
											.get(listproject.size() - 1));
								}
							}
						}
						break;
					case 2:
						if (listindustry.get(posion).getIsselected() == 0) {
							if (posion == listindustry.size() - 1) {
								for (int k = 0; k < listindustry.size(); k++) {
									listindustry.get(k).setIsselected(1);
									TextView textView = listview.get(1).get(k);
									textView.setBackgroundResource(R.drawable.xuanzhong);
									textView.setTextColor(Color.rgb(255, 255,
											255));
									if (!xuanzhongIndustry
											.contains(listindustry.get(k))) {
										xuanzhongIndustry.add(listindustry
												.get(k));
									}
								}
							} else {
								listindustry.get(posion).setIsselected(1);
								tv.setTextColor(Color.rgb(255, 255, 255));
								tv.setBackgroundResource(R.drawable.xuanzhong);
								if (!xuanzhongIndustry.contains(listindustry
										.get(posion))) {
									xuanzhongIndustry.add(listindustry
											.get(posion));
								}
							}
						} else {
							if (posion == listindustry.size() - 1) {
								for (int k = 0; k < listindustry.size(); k++) {
									listindustry.get(k).setIsselected(0);
									TextView textView = listview.get(1).get(k);
									textView.setTextColor(Color.rgb(66, 66, 66));
									if (k == listindustry.size() - 1)
										textView.setBackgroundResource(R.drawable.selectbg);
									else
										textView.setBackgroundResource(R.drawable.weixuanzhong);
									if (xuanzhongIndustry.contains(listindustry
											.get(k))) {
										xuanzhongIndustry.remove(listindustry
												.get(k));
									}
								}
							} else {
								listindustry.get(posion).setIsselected(0);
								tv.setTextColor(Color.rgb(66, 66, 66));
								if (posion == listindustry.size() - 1)
									tv.setBackgroundResource(R.drawable.selectbg);
								else
									tv.setBackgroundResource(R.drawable.weixuanzhong);
								if (xuanzhongIndustry.contains(listindustry
										.get(posion))) {
									xuanzhongIndustry.remove(listindustry
											.get(posion));
								}
								if (xuanzhongIndustry.contains(listindustry
										.get(listproject.size() - 1))) {
									TextView textView = listview.get(1).get(
											listview.get(1).size() - 1);
									listindustry.get(listindustry.size() - 1)
											.setIsselected(0);
									textView.setTextColor(Color.rgb(66, 66, 66));
									textView.setBackgroundResource(R.drawable.weixuanzhong);
									xuanzhongIndustry.remove(listindustry
											.get(listindustry.size() - 1));
								}
							}
						}
						break;
					case 3:
						if (listmodel.get(posion).getIsselected() == 0) {
							if (posion == listmodel.size() - 1) {
								for (int k = 0; k < listmodel.size(); k++) {
									listmodel.get(k).setIsselected(1);
									TextView textView = listview.get(2).get(k);
									textView.setBackgroundResource(R.drawable.xuanzhong);
									textView.setTextColor(Color.rgb(255, 255,
											255));
									if (!xuanzhongModel.contains(listmodel
											.get(k))) {
										xuanzhongModel.add(listmodel.get(k));
									}
								}
							} else {
								listmodel.get(posion).setIsselected(1);
								tv.setTextColor(Color.rgb(255, 255, 255));
								tv.setBackgroundResource(R.drawable.xuanzhong);
								if (!xuanzhongModel.contains(listmodel
										.get(posion))) {
									xuanzhongModel.add(listmodel.get(posion));
								}
							}
						} else {
							if (posion == listmodel.size() - 1) {
								for (int k = 0; k < listmodel.size(); k++) {
									listmodel.get(k).setIsselected(0);
									TextView textView = listview.get(2).get(k);
									textView.setTextColor(Color.rgb(66, 66, 66));
									if (k == listmodel.size() - 1)
										textView.setBackgroundResource(R.drawable.selectbg);
									else
										textView.setBackgroundResource(R.drawable.weixuanzhong);
									if (xuanzhongModel.contains(listmodel
											.get(k))) {
										xuanzhongModel.remove(listmodel.get(k));
									}
								}
							} else {
								listmodel.get(posion).setIsselected(0);
								tv.setTextColor(Color.rgb(66, 66, 66));
								if (posion == listmodel.size() - 1)
									tv.setBackgroundResource(R.drawable.selectbg);
								else
									tv.setBackgroundResource(R.drawable.weixuanzhong);
								if (xuanzhongModel.contains(listmodel
										.get(posion))) {
									xuanzhongModel.remove(listmodel.get(posion));
								}
								if (xuanzhongModel.contains(listmodel
										.get(listmodel.size() - 1))) {
									TextView textView = listview.get(2).get(
											listview.get(2).size() - 1);
									listmodel.get(listmodel.size() - 1)
											.setIsselected(0);
									textView.setTextColor(Color.rgb(66, 66, 66));
									textView.setBackgroundResource(R.drawable.weixuanzhong);
									xuanzhongModel.remove(listmodel
											.get(listmodel.size() - 1));
								}
							}
						}
						break;
					case 4:
						if (listjobs.get(posion).getIsselected() == 0) {
							if (posion == listjobs.size() - 1) {
								for (int k = 0; k < listjobs.size(); k++) {
									listjobs.get(k).setIsselected(1);
									TextView textView = listview.get(3).get(k);
									textView.setBackgroundResource(R.drawable.xuanzhong);
									textView.setTextColor(Color.rgb(255, 255,
											255));
									if (!xuanzhongJobs.contains(listjobs.get(k))) {
										xuanzhongJobs.add(listjobs.get(k));
									}
								}
							} else {
								listjobs.get(posion).setIsselected(1);
								tv.setTextColor(Color.rgb(255, 255, 255));
								tv.setBackgroundResource(R.drawable.xuanzhong);
								if (!xuanzhongJobs.contains(listjobs
										.get(posion))) {
									xuanzhongJobs.add(listjobs.get(posion));
								}
							}
						} else {
							if (posion == listjobs.size() - 1) {
								for (int k = 0; k < listjobs.size(); k++) {
									listjobs.get(k).setIsselected(0);
									TextView textView = listview.get(3).get(k);
									textView.setTextColor(Color.rgb(66, 66, 66));
									if (k == listjobs.size() - 1)
										textView.setBackgroundResource(R.drawable.selectbg);
									else
										textView.setBackgroundResource(R.drawable.weixuanzhong);
									if (xuanzhongJobs.contains(listjobs.get(k))) {
										xuanzhongJobs.remove(listjobs.get(k));
									}
								}
							} else {
								listjobs.get(posion).setIsselected(0);
								tv.setTextColor(Color.rgb(66, 66, 66));
								if (posion == listjobs.size() - 1)
									tv.setBackgroundResource(R.drawable.selectbg);
								else
									tv.setBackgroundResource(R.drawable.weixuanzhong);
								if (xuanzhongJobs.contains(listjobs.get(posion))) {
									xuanzhongJobs.remove(listjobs.get(posion));
								}
								if (xuanzhongJobs.contains(listjobs
										.get(listjobs.size() - 1))) {
									TextView textView = listview.get(3).get(
											listview.get(3).size() - 1);
									listjobs.get(listjobs.size() - 1)
											.setIsselected(0);
									textView.setTextColor(Color.rgb(66, 66, 66));
									textView.setBackgroundResource(R.drawable.weixuanzhong);
									xuanzhongJobs.remove(listjobs.get(listjobs
											.size() - 1));
								}
							}
						}
						break;

					default:
						break;
					}

				}
			});

			Paint paint = new Paint();
			paint.setTextSize(textview.getTextSize());
			float size = paint.measureText(textview.getText().toString());
			view.setTag((int) size + Utils.dip2px(this, 10 * 2 + 10));
			textview.setTag(i);
			listText.add(textview);
			listtextview.add(view);
		}
		listview.add(listText);

		int j = 0;
		LinearLayout llayout = null;
		int MaxWidth = MainActivity.widthPixels - Utils.dip2px(this, 20);
		int temporarywidth = 0;
		while (true) {
			View view = null;
			int tagwidth = 0;
			if (j == listtextview.size()) {
				return;
			} else {
				view = listtextview.get(j);

				tagwidth = (Integer) view.getTag();
			}
			if (j == 0) {
				llayout = new LinearLayout(this);
			}
			System.out.println("textview:" + tagwidth + "temporarywidth:"
					+ temporarywidth + "MaxWidth:" + MaxWidth);
			if (tagwidth + temporarywidth + 40 < MaxWidth) {
				temporarywidth += tagwidth;
				llayout.addView(view);
			} else {
				layer.addView(llayout);

				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT,
						LinearLayout.LayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(0, 15, 0, 0);

				llayout = new LinearLayout(this);
				llayout.setLayoutParams(layoutParams);
				temporarywidth = (Integer) view.getTag();

				llayout.addView(view);
			}

			if (j == listtextview.size() - 1) {
				layer.addView(llayout);
			}
			j++;
		}
	}

	private void selectData_info(List<RetrieveInfoModel> listModel,
			String tabname, String selection, int type) {

		DatabaseHelper dh = new DatabaseHelper(this);
		SQLiteDatabase db = dh.getReadableDatabase();
		Cursor cursor = db.query(tabname, null, selection, null, null, null,
				null);
		while (cursor.moveToNext()) {
			RetrieveInfoModel rim = new RetrieveInfoModel();
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String name = cursor.getString(cursor.getColumnIndex("name_cn"));
			int isselected = cursor.getInt(cursor.getColumnIndex("isselected"));

			rim.setId(id);
			rim.setName(name);
			rim.setIsselected(isselected);
			listModel.add(rim);
		}

		switch (type) {
		case 1:
			addlastInfo(listModel, "有活就干");
			break;
		case 2:
			addlastInfo(listModel, "哪行都行");
			break;
		case 3:
			addlastInfo(listModel, "啥都能整");
			break;
		case 4:
			addlastInfo(listModel, "放哪都成");
			break;

		default:
			break;
		}
		cursor.close();
		db.close();
		dh.close();

	}

	private void addlastInfo(List<RetrieveInfoModel> listModel, String name) {
		boolean flag = true;
		for (int i = 0; i < listModel.size(); i++) {
			if (listModel.get(i).getIsselected() == 0)
				flag = false;
		}
		RetrieveInfoModel rim = new RetrieveInfoModel();
		rim.setId(-1);
		rim.setName(name);
		if (flag)
			rim.setIsselected(1);
		else
			rim.setIsselected(0);
		listModel.add(rim);
	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {

	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.retrieve_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.next:
			if (xuanzhongProject.size() == 0) {
				Toast.makeText(this, "必须选择一个周期！", Toast.LENGTH_LONG).show();
				return;
			}
			if (xuanzhongIndustry.size() == 0) {
				Toast.makeText(this, "必须选择一个行业！", Toast.LENGTH_LONG).show();
				return;
			}
			if (xuanzhongModel.size() == 0) {
				Toast.makeText(this, "必须选择一个模块！", Toast.LENGTH_LONG).show();
				return;
			}
			if (xuanzhongJobs.size() == 0) {
				Toast.makeText(this, "必须选择一个岗位！", Toast.LENGTH_LONG).show();
				return;
			}
			// System.out.println("xuanzhongProject长度:" +
			// xuanzhongProject.size());
			String Project = "\"projectCycle\":\"";
			String Industry = "\"projectIndustry\":\"";// + "\"}"
			String Model = "\"model\":\"";
			String Jobs = "\"station\":\"";
			for (int i = 0; i < xuanzhongProject.size(); i++) {
				// System.out.println("-----" +
				// xuanzhongProject.get(i).getName());
				String douhao = ",";
				if (i == xuanzhongProject.size() - 1) {
					douhao = "\",";
				}
				Project = Project + xuanzhongProject.get(i).getId() + douhao;
			}

			// System.out.println("xuanzhongIndustry长度:"+
			// xuanzhongIndustry.size());
			for (int i = 0; i < xuanzhongIndustry.size(); i++) {
				// System.out.println("-----" +
				// xuanzhongIndustry.get(i).getName());
				String douhao = ",";
				if (i == xuanzhongIndustry.size() - 1) {
					douhao = "\",";
				}
				Industry = Industry + xuanzhongIndustry.get(i).getId() + douhao;
			}

			// System.out.println("xuanzhongModel长度:" + xuanzhongModel.size());
			for (int i = 0; i < xuanzhongModel.size(); i++) {
				// System.out.println("-----" +
				// xuanzhongModel.get(i).getName());
				String douhao = ",";
				if (i == xuanzhongModel.size() - 1) {
					douhao = "\",";
				}
				Model = Model + xuanzhongModel.get(i).getId() + douhao;
			}

			// System.out.println("xuanzhongJobs长度:" + xuanzhongJobs.size());
			for (int i = 0; i < xuanzhongJobs.size(); i++) {
				// System.out.println("-----" + xuanzhongJobs.get(i).getName());
				String douhao = ",";
				if (i == xuanzhongJobs.size() - 1) {
					douhao = "\",";
				}
				Jobs = Jobs + xuanzhongJobs.get(i).getId() + douhao;
			}

			String str = "{" + Project + Industry + Model + Jobs;

			System.out.println("str:" + str);

			Intent intent = new Intent(RetrieveActivity.this,
					CityActivity.class);
			intent.putExtra("result", str);
			intent.putExtra("type", 1);
			startActivityForResult(intent, 0);

			break;
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			setResult(99, data);
			finish();
		default:
			break;
		}
	}
}
